package configuration;

import java.util.HashMap;

/**
 * Replaces special xml characters with their xml compatible translations.
 * 
 * @author ashepard
 * 
 */

public class XMLEncoder {

	private static HashMap<String, String> codes;

	static {
		codes = new HashMap<String, String>();
		codes.put("\"", "&quot;");
		codes.put("&", "&amp;");
		codes.put("'", "&apos;");
		codes.put("<", "&lt;");
		codes.put(">", "&gt;");
	}

	/**
	 * Constructor for XMLEncoder
	 */
	private XMLEncoder() {
	}

	/**
	 * Replaces (encodes) symbols input to function with XML equivalents.
	 * 
	 * @param input
	 *            Original input to encode.
	 * @return Equivalent XML code.
	 */
	public static String encode(String input) {
		for (String x : codes.keySet()) {
			input = input.replace(x, codes.get(x));
		}
		return input;
	}

}
